package cn.edu.nuc.weiboclient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oauth.signpost.OAuth;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import cn.edu.nuc.modle.WeiBoData;
import cn.edu.nuc.modle.WeiBoList;

import com.google.gson.Gson;


public class UsermentionedTimeLine extends ListActivity{
	private SharedPreferences prefs;
	private WeiBoItemAdapter adapter = null;
	private ListView listView = null;
	private ImageView enter = null;

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem followsItem = menu.add(Menu.NONE, 1, 1, "Home");
		MenuItem mainItem = menu.add(Menu.NONE, 0, 0, "ME");
		MenuItem friendsItem = menu.add(Menu.NONE, 2, 2, "Friends");
		MenuItem homepageItem = menu.add(Menu.NONE, 3, 3, "Funs");
		MenuItem otherUsersItem = menu.add(Menu.NONE,4, 4, "LoginInterface");
		MenuItem usermentionedItem = menu.add(Menu.NONE, 5, 5, "Exit");
		
		followsItem.setIcon(android.R.drawable.ic_menu_send);
		friendsItem.setIcon(android.R.drawable.ic_menu_more);
		homepageItem.setIcon(android.R.drawable.ic_menu_send);
		otherUsersItem.setIcon(android.R.drawable.ic_menu_more);
		usermentionedItem.setIcon(android.R.drawable.ic_menu_send);
		mainItem.setIcon(android.R.drawable.ic_media_rew);
		return super.onCreateOptionsMenu(menu);
	}
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if(item.getItemId() == 5){
			finish();
			System.exit(0);
		}
		else if(item.getItemId() == 1){
			Intent intent = new Intent();
			intent.setClass(this,HomepageTimeLine.class).setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 2){
			Intent intent = new Intent();
			intent.setClass(this, FriendsTimeLineActivity.class);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 3){
			Intent intent = new Intent();
			intent.setClass(this, FollowsTimeLine.class);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 4){
			Intent intent = new Intent();
			intent.setClass(this, WeiboClientActivity.class).setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 0){
			Intent intent = new Intent();
			intent.setClass(this,MyPostTimeLine.class);
			startActivity(intent);
			finish();
		}
		return super.onMenuItemSelected(featureId, item);
	}
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.followlist);
		enter = (ImageButton)findViewById(R.id.enter);
		enter.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
            	intent.setClass(UsermentionedTimeLine.this,Enter.class);
            	startActivity(intent);
            	finish();
				
			}
		});
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		Map<String,String> keyValues = new HashMap<String,String>();
		keyValues.put("format", "json");
		keyValues.put("pageflag", "0");
		keyValues.put("pagetime", "0");
		keyValues.put("reqnum", "5");
		WeiBoClient weiBoClient = new WeiBoClient(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET, prefs.getString(OAuth.OAUTH_TOKEN, ""), prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""));
		String result = weiBoClient.doGet(Constants.WeiBoApi.MENTIONS_TIMELINE, keyValues);
		Gson gson = new Gson();
		WeiBoList weiBoList = gson.fromJson(result, WeiBoList.class);
		if(weiBoList.getData()!= null){
		List<WeiBoData> weiBoData = weiBoList.getData().getInfo();
		adapter = new WeiBoItemAdapter(this, weiBoData);
		listView = getListView();
		listView.setAdapter(adapter);	
		listView.setOnItemClickListener(new WeiBoOnClickItemButtonListener());
		}
		else{
			System.out.println("Data is null");
		}	
	}	
	class WeiBoOnClickItemButtonListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parten, View view, int position,
				long id) {
//			Map<String,String> map = (Map<String, String>)parten.getItemAtPosition(position);
//			//Intent intent = new Intent();
//			String name = map.get("name");
//			System.out.println(name);
			System.out.println(parten.getItemAtPosition(position));
//			System.out.println(parten.getItemIdAtPosition(position));
//			//intent.putExtra("headImage", map.get("headImage"));
		WeiBoData data = (WeiBoData)parten.getItemAtPosition(position);
			Intent intent = new Intent();
			if(data.getHead() != null){
				intent.putExtra("headImage", data.getHead());
			}
			if(data.getImage() != null){
				List<String> image = data.getImage();		
				intent.putExtra("image", image.get(0).toString());
			}
				intent.putExtra("nick", data.getNick());
				intent.putExtra("name", data.getName());
				intent.putExtra("tweet", data.getText());
				intent.putExtra("time", data.getTimestamp());
				intent.setClass(UsermentionedTimeLine.this, ShowOneWeiBo.class);
				startActivity(intent);
		}
		
	}
}
